//
//  ImageTitleButton.m
//  mainModule
//
//  Created by mac on 2025/2/14.
//

#import "ImageTitleButton.h"

@implementation ImageTitleButton

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/
- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        [self setup];
    }
    return self;
}

- (void)setup {
    self.titleLabel.textAlignment = NSTextAlignmentCenter;
    self.imageView.contentMode = UIViewContentModeScaleAspectFit;
}

- (void)layoutSubviews {
    [super layoutSubviews];
    CGRect imageRect = self.imageView.frame;
    CGRect titleRect = self.titleLabel.frame;
    CGFloat totalHeight = imageRect.size.height + titleRect.size.height + 5; // 5 是间距
    imageRect.origin.x = (self.bounds.size.width - imageRect.size.width) / 2;
    imageRect.origin.y = (self.bounds.size.height - totalHeight) / 2;
    self.imageView.frame = imageRect;
    titleRect.origin.x = (self.bounds.size.width - titleRect.size.width) / 2;
    titleRect.origin.y = imageRect.origin.y + imageRect.size.height + 5;
    self.titleLabel.frame = titleRect;
}
@end
